/*
 * @Author: zxc
 * @Date: 2021-01-23 15:20:43
 * @LastEditTime: 2021-01-23 15:54:23
 * @LastEditors: zxc
 * @Description:纯函数
 */
import _ from "lodash"
const log = console.log
// let numbers = [1, 2, 3, 4, 5] // 纯函数
// console.log(numbers.slice(0, 3))
// console.log(numbers.slice(0, 3))
// console.log(numbers.slice(0, 3))
// // 不纯的函数
// console.log(numbers.splice(0, 3))
// console.log(numbers.splice(0, 3))
// console.log(numbers.splice(0, 3))

/**
 * @Author: zxc
 * @Date: 2021-01-23 15:44:46
 * @func 可缓存的纯函数
 */
const getArea = r => {
  log(r)
  return Math.PI * r * r
}

// let getAreaWithMemory = _.memoize(getArea)
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))

const memoize = fn => {
  let cache = {}
  return (...args) => {
    const key = JSON.stringify(args)
    cache[key] = cache[key] || fn.apply(fn, args)
    return cache[key]
  }
}
// let getAreaWithMemory = memoize(getArea)
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
// log(getAreaWithMemory(3))
